package com.exsun.gateway.jt809.kafka.gps;

import exsun.jt808.data.jt809_data.BasicVehicleInfo;
import exsun.jt808.data.jt809_data.alarm.main.UpWarnMsgAdptInfo;
import exsun.jt808.data.jt809_data.alarm.main.UpWarnMsgInfoEX;
import exsun.jt808.data.jt809_data.alarm.main.WarnType;
import exsun.jt808.data.jt809_data.alarm.main.WarnTypeDescription;
import exsun.jt808.data.jt809_data.vehicledynamic.GnssData;
import exsun.jt808.data.upload_data.GpsAlarmType;
import exsun.jt808.data.upload_data.GpsData;
import exsun.jt808.data.upload_data.GpsPropertyType;
import exsun.jt808.data.upload_data.jt808.AlarmDetail;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author:
 * @Date:
 * @Description: 位置信息中报警信息的处理（针对809报警信息的上报）
 */
public class GpsConvert {

    public static List<UpWarnMsgAdptInfo> alarmHandle(GpsData positionInfo, BasicVehicleInfo basicVehicleInfo) {
        List<UpWarnMsgAdptInfo> upWarnMsgAdptInfoList = new ArrayList<>();

        Date time = positionInfo.gdt;
        int alarmCount = 0;
        if (positionInfo.alm.contains(GpsAlarmType.OverSpeed)) {
            upWarnMsgAdptInfoList.add(new UpWarnMsgAdptInfo(WarnType.OVER_SPEED_ALARM,
                    time, WarnTypeDescription.OVER_SPEED_ALARM_DESCRIP, basicVehicleInfo));
            alarmCount++;
        }
        if (positionInfo.alm.contains(GpsAlarmType.FatigueDriving)) {
            upWarnMsgAdptInfoList.add(new UpWarnMsgAdptInfo(WarnType.FATIGUE_DRIVING_ALARM,
                    time, WarnTypeDescription.FATIGUE_DRIVING_ALARM_DESCRIP, basicVehicleInfo));
            alarmCount++;
        }
        if (positionInfo.alm.contains(GpsAlarmType.Emergency)) {
            upWarnMsgAdptInfoList.add(new UpWarnMsgAdptInfo(WarnType.EMERGENCY_ALARM,
                    time, WarnTypeDescription.EMERGENCY_ALARM_DESCRIP, basicVehicleInfo));
            alarmCount++;

        }
        if (positionInfo.alm.contains(GpsAlarmType.InOutOfArea)) {
            upWarnMsgAdptInfoList.add(new UpWarnMsgAdptInfo(WarnType.ENTER_ZONE_ALARM,
                    time, WarnTypeDescription.ENTER_ZONE_ALARM_DESCRIP, basicVehicleInfo));
            alarmCount++;
        }
        if (positionInfo.alm.contains(GpsAlarmType.IllegalVehicleIgnition)) {
            upWarnMsgAdptInfoList.add(new UpWarnMsgAdptInfo(WarnType.GUARDING_ALARM,
                    time, WarnTypeDescription.GUARDING_ALARM_DESCRIP, basicVehicleInfo));
            alarmCount++;
        }
        if (positionInfo.alm.contains(GpsAlarmType.RouteDeviation)) {
            upWarnMsgAdptInfoList.add(new UpWarnMsgAdptInfo(WarnType.ROUTE_DEVAITE_ALARM,
                    time, WarnTypeDescription.ROUTE_DEVAITE_ALARM_DESCRIP, basicVehicleInfo));
            alarmCount++;
        }
        for (int alarm:positionInfo.alm) {
            if (alarm != GpsAlarmType.OverSpeed&&alarm != GpsAlarmType.FatigueDriving&&
                    alarm != GpsAlarmType.Emergency&&alarm != GpsAlarmType.InOutOfArea&&
                    alarm != GpsAlarmType.IllegalVehicleIgnition&&alarm != GpsAlarmType.RouteDeviation) {
                upWarnMsgAdptInfoList.add(new UpWarnMsgAdptInfo(WarnType.OTHER_ALARM,
                        time, WarnTypeDescription.OTHER_ALARM_DESCRIP, basicVehicleInfo));
            }
        }

        return upWarnMsgAdptInfoList;
    }

    public static GnssData gnssDataHandle(GpsData positionInfo) {
        GnssData gnssData = new GnssData();
        gnssData.setAltitude(positionInfo.hgt);
        gnssData.setLat(positionInfo.lat);
        gnssData.setLon(positionInfo.lng);
        gnssData.setDate(positionInfo.gdt);
        gnssData.setDirection(positionInfo.dre);
        gnssData.setVec1(positionInfo.spd);
        int vec2 = positionInfo.pop.containsKey(GpsPropertyType.DriveSpeed) ? Integer.parseInt(positionInfo.pop.get(GpsPropertyType.DriveSpeed)): positionInfo.spd;
        gnssData.setVec2(vec2);
        gnssData.setMileage(positionInfo.mie);
        gnssData.setState(positionInfo.stm);
        gnssData.setAlarm(positionInfo.amm);
        return gnssData;
    }

    public static Map<Integer,String> alarmMap1 (Map<Integer,String> map){
        map.put(0X0001,"超速报警");
        map.put(0X0002,"疲劳驾驶报警");
        map.put(0X0003,"紧急报警");
        map.put(0x0102,"摄像头遮挡报警");
        map.put(0x0201,"前向碰撞报警");
        map.put(0x0202,"车道偏离报警");
        map.put(0x0203,"车距过近报警");
        map.put(0x0211,"生理疲劳驾驶报警");
        map.put(0x0212,"长时间不目视前方报警");
        map.put(0x0213,"抽烟报警");
        map.put(0x0214,"接打手持电话报警");
        map.put(0x0215,"驾驶员不在驾驶位置报警");
        map.put(0x0216,"双手同时脱离方向盘报警");
        return map;
    }

    public static Map<Integer,Integer> alarmMap2 (Map<Integer,Integer> map){
        map.put(1,0X0001);
        map.put(2,0X0002);
        map.put(0,0X0003);
        map.put(245,0x0102);
        map.put(25,0x0201);
        map.put(26,0x0202);
        map.put(27,0x0203);
        map.put(34,0x0211);
        map.put(37,0x0212);
        map.put(36,0x0213);
        map.put(35,0x0214);
        map.put(38,0x0215);
        map.put(52,0x0216);
        return map;
    }

    public static List<UpWarnMsgInfoEX> alarmMsgInfoEX(GpsData positionInfo, BasicVehicleInfo basicVehicleInfo) {
        String pattern = "yyyy-MM-dd'T'HH:mm:ss'Z'";
        SimpleDateFormat simpleDateFormat =
                new SimpleDateFormat(pattern);
        simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));

        List<UpWarnMsgInfoEX> list = new ArrayList<>();
        Map<Integer,String> map1 = new HashMap<>();
        Map<Integer,Integer> map2 = new HashMap<>();
        alarmMap1(map1);
        alarmMap2(map2);
        for (int alarmType : positionInfo.adl.keySet()) {
            AlarmDetail alarmDetail = positionInfo.adl.get(alarmType);
            UpWarnMsgInfoEX infoEX = new UpWarnMsgInfoEX();
            infoEX.setBasicVehicleInfo(basicVehicleInfo);
            for (int type : map2.keySet()){
                for (int key : map1.keySet()){
                    if (type == alarmType){
                        infoEX.setWarnType(map2.get(type));
                        if (key == map2.get(type)){
                            String value = map1.get(key);
                            infoEX.setInfoContent(value);
                        }
                    }
                }
            }
            infoEX.setWarnTime(new Date().getTime());
            infoEX.setInfoId(alarmDetail.afg);
            infoEX.setDriver("tom1");
            infoEX.setDriverNo("1234");
            infoEX.setLevel(alarmDetail.level);
            infoEX.setLon(positionInfo.lng);
            infoEX.setLat(positionInfo.lat);
            infoEX.setAltitude(positionInfo.hgt);
            infoEX.setVec1(positionInfo.spd);
            String vec2 = positionInfo.GetProperty(GpsPropertyType.DriveSpeed);
            infoEX.setVec2( vec2 == null ? positionInfo.spd: Double.parseDouble(vec2));
            infoEX.setStatus(1);
            infoEX.setDirection(positionInfo.dre);
//            for (int key : map1.keySet()) {
//                for (int warnType : map2.keySet()) {
//                    if (key == map2.get(warnType)){
//                        String value = map1.get(key);
//                        infoEX.setInfoContent(value);
//                    }
//                }
//            }
            list.add(infoEX);
        }
        return list;
    }
}
